

em.tasks4user = new function() {
// function em.tasks4user() {
	var class_CTask = 1;
	this.objName = 'tasks4user : ';

	var self = this;
	this.actionSeq = new CActionSeq();
	
	
	this.task_cbs= { 
				success_cb : function(code, msg) {
					debug_log(" TASKS :: success CB is called !!! ") ;
				}, 
				error_cb : function(errorcode) {
					debug_log("TASKS  Erro CB is called !!! in test case :" + errorcode) ;
				},
				notify_cb : function(code, msg) {
					em.util.print_result("TASKS " + code + msg );
				}, 
	};
	
	
		// 	===================================================================
//         Login New User 
	

	// 癒쇱� select user 瑜���寃껋쑝濡�媛�젙... 
	// assume : before  select user is done.. 
	this.processLogin = function(username, pw) {
		var outer = this;
		
		var curuser_info =  CDB.getCurUserInfo();
		this.caller = arguments.callee.caller;
		this.cb = em.cbs.userlogin;
		 
		 // test :	 /// call cb result 
 		 // em.cbs.userlogin.fireWith(arguments.caller , ['start login cb msg']);
	
		var bPvp = 0;
		//  greate pvp  임시 처리
		if ( username == 'greate_PVP' ) { 
			username = 'Greateload';
			bPvp = 1;
			
		}
		
		// only for   ::  try login ::  
		if ( pw != undefined) {
			curuser_info = curuser_info || {};
			curuser_info.password = pw;
			curuser_info.device_code = 'e7e8b613d1b08a603924563cb2c3ea24fd23fc2e';
			debug.log( this, ' pass :', pw, ' set: ', curuser_info.password , curuser_info.device_code);
		}
		/*
			GET /info.php?
jsonpcallback=jsonp1323389966359&_=1323389984249
&user=Greateload
&action=login
&pvp=0
&code=e7e8b613d1b08d069751171da8b0eb17fe02ee8c				// iphone kr 

		*/
		var login_action = {
			name: 'Login',
			start: undefined,
			param: this.task_cbs,		// for cb functions 
			action_table: [
				{ 	
					cmd: 'login_info',
					preAction:  undefined,
					doParam: { user: username , login_action: 'login', pvp: bPvp, 
									device_code: curuser_info.device_code,
									nosessionkey: true, 
									cb : function (data) {
										checkLoginInfo.call(outer, data); }
								},
					doFunc:	 'login_info',
					gotResult: this.actionAResult,
				},
				
/*
	GET /game/
login_api.php?
jsonpcallback=jsonp1323389966360&_=1323389985557
&username=Greateload
&password=1212
&_l=kr&_p=EW-IPHONE-KR HTTP/1.1
		
		
jsonp1323389966360(
	{"code":0,"ret":{"key":"b557990e3591188901cb50f1422bc0d4"}})
*/				
				{ 
					cmd: 'login_api',
					preAction:  undefined,
					// doParam: { username: curuser_info.name, password: curuser_info.password, nosessionkey: true, cb : function (data) {
					doParam: { username: username, password: curuser_info.password, nosessionkey: true, cb : function (data) {
									checkLoginApi.call(outer, data); }
								},
					doFunc:	 'login_api',
					gotResult: this.actionAResult,		// �대뵒��?? 
				},		
				]
		};
	
		
		this.actionSeq.setAction(login_action);
		this.actionSeq.start();
	};
	

	
	var checkLoginInfo = function(data) {
			// {"code":0,"ret":{
		// "server":"http:\/\/k2.emrosswar.com\/",
		// "user":"Greateload","referer":"feelcool","refercode":"Greateload"}})
		debug.log(this,CDB.getCurUser(), data);

 		 
		if ( data.code != 0 ) {
			debug.log(self, 3, ' LOGIN CB Data Error !!! ');
			em.cbs.userlogin.fire('ERROR.LOGIN_INFO', data);
			return;
		}

		// call new user with server 
		em.cbs.userlogin.fire('DATA.server', data.ret.server);
				
		if ( CDB.getCurUser() != data.ret.user) {
			// just warning !!!
			debug.log(self, 3, '  !!! Not Match User Name !!! ', CDB.getCurUser() ,  data.ret.user);
			// return;
		}
		var outer = self;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500));
	};
	
	
	var checkLoginApi = function(data) {
		// {"code":0,"ret":{"key":"b557990e3591188901cb50f1422bc0d4"}})
		var outer = self;
		
		if (data.code != 0) {
			debug.log (self, 3,  ' CB data got  checkLoginApi:: ERROR !!!!! ');
			em.cbs.userlogin.fire('ERROR.LOGIN_API', data);
			return;
		}
		
		// call new user with new key 
		em.cbs.userlogin.fire('DATA.key', data.ret.key);
		
		debug.log (self, 3,  ' CcheckLoginApi:: Got New Key :: Store !!!!!!! ' + data.ret.key);
		em.util.print_result(' CcheckLoginApi:: Got New Key :: Store !!!!!!! ' + data.ret.key);
		
		
				// test :	 /// call cb result 
 		// this.cb.fireWith(this.caller , ['got key']);
 		
		//  start : start user fucntio 
		// setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(2000));
		setTimeout( function() { outer.startGM.call(outer); } ,CUtil.RandTime(500));
	};
	
		
	// 	===================================================================
//         start G M 
	/*
		GET /game/
get_userinfo_api.php?
jsonpcallback=jsonp1323389966361&_=1323389987156
&key=b557990e3591188901cb50f1422bc0d4
&_l=kr
&_p=EW-IPHONE-KR HTTP/1.1
			

jsonp1323389966361(
{"code":0,"ret":{
	"user":{"id":18949,"nick":"greateload","nationid":1,"level":42,"money":345,"guild":"greate","guildid":262,
		"gpower":1,"gflag":97,"protection":0,"p_end":1322486844,"status":0,
		"city":[
			{"id":22289,"name":"centercast","landtype":3,"x":61,"y":19},
			{"id":22336,"name":"heaven","landtype":4,"x":61,"y":18},
			{"id":22349,"name":"Spacecastl","landtype":2,"x":60,"y":17},
			{"id":22423,"name":"Forward","landtype":5,"x":62,"y":17}],
		"conq":[0,0,null,5],
		"gift":[{"id":168,"msg":"","s":0}],"pvp":0
		},
	"events":[
		{"id":1,"icon":"gem.jpg","name":"\uc131\uc2e4\ud55c \uad70\uc8fc\ub97c \uc704\ud55c \ubcf4\uc0c1! [5\/7 \uc77c\uac04]","desc":"<b>\uc624\ub298\uc758 \ubcf4\uc0c1\uc740<\/b> \ub85c\uadf8\uc778 \ud6c4 \ubc1b\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4. 7\uc77c\uac04 \uc5f0\uc18d\uc73c\ub85c \ub85c\uadf8\uc778\ud558\uba74 \ub354 \uc2b9\ub9ac\ud560 \uc218 \uc788\uc744 \uac83\uc785\ub2c8\ub2e4.<b>\uac15\ub825\ud55c \uc601\uc9c0 \uad6c\ucd95\uc744 \uc704\ud558\uc5ec!<\/b> \ubb34\ub8cc!"},{"id":2,"icon":"0.png","name":"\ub808\ubca8 \uaca9\ucc28\ub294 \ud65c\ub3d9\uc801\uc778 \uad70\uc8fc\ub4e4\uc744 \ubcf4\ud638\ud569\ub2c8\ub2e4.","desc":"\uba3c\uc800 \uacf5\uaca9\ud558\uc9c0 \uc54a\ub294 \ud55c, 10\ub808\ubca8 \uc774\uc0c1\uc778 \uad70\uc8fc\ub4e4\ub85c\ubd80\ud130 \ubcf4\ud638\ubc1b\uc2b5\ub2c8\ub2e4. \uc774 \ubc84\ud504\ub294 24\uc2dc\uac04 \uc774\uc0c1 \uc624\ud504\ub77c\uc778\uc77c \uacbd\uc6b0 \uc801\uc6a9\ub418\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4."}
		],
	"status":{"invite":1,"ver":0.9,"vs":1.25,"vr":0.5,"ce":1,"he":1}}})
	*/
	var checkUserInfo = function(data) {
		
		if (data.code != 0) {
			debug.log (self, 3,  ' checkUserInfo:: ERROR !!!!! ');
			em.cbs.userlogin.fire('ERROR.startGM', 'error key!!! ');
			return;
		}

		// TODO:  info update 는  cstl mgr 로..!!! 		
		// call cb user info 
		em.cbs.userlogin.fire('DATA.userinfo', data.ret.user);
		
		// call cb cstls info 
		em.cbs.userlogin.fire('DATA.cstlsinfo', data.ret.user.city);
		
		

		// em.accmgr.update
		// var userinfo = CDB.getCurUserInfo();
		// em.util.print_result( em.util.print_obj(data.ret.user.city));
		// $.extend( userinfo.castles , data.ret.user.city);
		
		var outer = self;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500));		
	} ;
	/*
			{"quest":[
		{"id":48,"grade":0,"status":0,"done":1},
		{"id":49,"grade":0,"status":1,"done":1},
	*/
	var checkSystemTask = function(data) {
		var outer = this;
		// update acc mgr 
		em.cbs.userlogin.fire('DATA.questinfo', data.ret);
		setTimeout( function() { em.cstlmgr.moveCstlAt.call(em.cstlmgr,0); } ,CUtil.RandTime(500));
	};
	
	this.startGM = function() {
		var outer = this;
		
		var startgm_action = {
			name: 'startGM',
			// start: undefined,
			// param: this.task_cbs,		// for cb functions 
			action_table: [
				{ 
					cmd: 'get_userinfo_api',
					// preAction:  undefined,
					doParam: { 	cb : function (data) {
									checkUserInfo.call(outer, data); }
								},
					doFunc:	 'get_userinfo_api',
				},
				
/*
GET /game/
system_task_api.php?
jsonpcallback=jsonp1323389987755&_=1323389988001&key=b557990e3591188901cb50f1422bc0d4
&action=task_list
&_l=kr&_p=EW-IPHONE-KR HTTP/1.1

result .. .ignore !! 
*/				
				{ 
					cmd: 'system_task_api',
					doParam: { action: 'task_list', cb : function (data) {
									checkSystemTask.call(outer, data); }
								},
					doFunc:	 'system_task_api',
				},		
				]
		};
	
		
		// set pvp action 
		if ( CDB.getCurUserOptions().pvp ) {
			startgm_action.action_table[0] = {
					cmd: 'pvp_get_userinfo_api',
					preAction:  undefined,
					doParam: { pushid : CDB.getCurUserInfo().pushid, 	
							cb : function (data) {
									checkUserInfo.call(outer, data); }
								},
					doFunc:	 'pvp_get_userinfo_api',
					gotResult: this.actionAResult,
			};
		}
		
		this.actionSeq.setAction(startgm_action);
		this.actionSeq.start();
	};
	
	
// 	===================================================================
//          move next cstl 
			/*
			jsonp1323389987756({"code":0,"ret":{"cdlist":[
{"id":843558,"cdtype":1,"target":9,"owner":0,"secs":17539},    //  uni  
{"id":846316,"cdtype":1,"target":10,"owner":0,"secs":69911},	// arena 
{"id":847570,"cdtype":1,"target":13,"owner":0,"secs":26386},   	// fuci
{"id":220951,"cdtype":2,"target":7,"owner":87575,"secs":75168}, //   study  2 ex Arm  7  h id 
{"id":192011,"cdtype":3,"target":15,"owner":152,"secs":4111}    // train  o   3   gid  
],"grade":42,"money":345}})	
		*/
/*
	var checkCDlist =function(data) {

		console.log(data);
		
		// store cd list to  0 th cstl  
		var cstls = CDB.getCurUserCtsl();

		// replace 占쎈똻鍮�占쏙옙 
		cstls[0].cdlist = data.ret.cdlist;
		// $.extend(cstls[0] , data.ret);
		 
	};
	*/

	
	// ===============================================
	//  move cstl
	// not a seq action..  
	this.moveCstl = function(target_cstl, dfd) {
		
		this.done_step = 2;
		this.cur_step = 0;
		
		// set dfd 
		this.cstl_dfd = dfd;
		
		debug.log( this, 'moveCstl : target_cstl id : ' + target_cstl.id );
		// use action helper
		var outer = this;
		var paramcity = { citnum: target_cstl.id ,  cb : function (data) {
								cbMoveCSTL_city.call(outer, data); } };
		em.ahelper.getCitInfo(paramcity);
		var paramcd = { citnum: target_cstl.id ,  cb : function (data) {
								cbMoveCSTL_cdinfo.call(outer, data); } }; 
		em.ahelper.getCDInfo(paramcd);

	};
	
	var cbMoveCSTL_city = function(data) {
		if ( data.code != 0 ){
			// error !!! 
			debug.log(this, 'cbMoveCSTL_city ret error ' );
			this.cstl_dfd.reject();
			return;
		}

		// update city info to cstl mgr 
		em.cstlmgr.updateCstl(data.ret.city);
		em.accmgr.updateGrade(data.ret.grade);
		// chgeck action compolete		
		if ( ++this.cur_step == this.done_step) {
			this.cstl_dfd.resolve();
		}

	};
	
	var cbMoveCSTL_cdinfo = function(data){
		if ( data.code != 0 ){
			// error !!!
			debug.log(this, 'cbMoveCSTL_cdinfo ret error ' );
			this.cstl_dfd.reject();
		} else 
		{
			
		
			// update cd list to cstl mgr 
			em.cstlmgr.updateCdlist(data.ret.cdlist);
			
			if ( ++this.cur_step == this.done_step) {
				this.cstl_dfd.resolve();
			}
			
			// run registered after action 
			// this.move1done_dfd.resolve(data);
			
			// em.hntmgr.fireEvent('cstlchanged');
			
		}
	};
	
	


};